package tice.twitterwalk.List;

import tice.twitterwalk.App;
import tice.twitterwalk.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ListView;
import android.widget.ViewFlipper;

public class TweetsListView extends ListView {
	
    private static final int SWIPE_MIN_DISTANCE = 150;
    private static final int SWIPE_MAX_OFF_PATH = 100;
	private static final int SWIPE_THRESHOLD_VELOCITY = 200;
	private GestureDetector gestureDetector;
	private Animation slideLeftIn;
	private Animation slideLeftOut;
	private Animation slideRightIn;
    private Animation slideRightOut;
    private ViewFlipper viewFlipper;
    private Context mCtx;
    
	public App _App = null;
	public TweetsCursorAdapter _Items;
	public int _CurrentThread = -1;
	public int _ViewType;
	public int _TweetsCount;
	
/*	
    public int GetListViewType(){
    	return _ViewType;
    }
    
    public void SetListViewType(int type){
    	_ViewType = type;
    }
    
    public void SetCurrentThread(int position){
    	if(position >= 0 && position < _Items.getCount()){
    		_CurrentThread = position;
    	}
    }
    
    public int GetCurrentThread(){
    	return _CurrentThread;
    }
    
    public TweetsCursorAdapter GetCursor(){
    	return _Items;
    }
*/    
	public TweetsListView(Context context) {
		super(context);

		mCtx = context;
		
		_App = (App)context.getApplicationContext();
		
	    TypedArray atts = context.obtainStyledAttributes(new int []{R.attr.ListViewBackground,R.attr.ListViewDivider});
	    
	    _Items = new TweetsCursorAdapter(context, null);

 		setAdapter(_Items);
 		setSelection(0);
        setFadingEdgeLength(2);
        setClickable(true);
        setFocusable(true);
        setFadingEdgeLength(0);
        setSelector(R.drawable.shape_5);
        setDrawSelectorOnTop(true);
        setAlwaysDrawnWithCacheEnabled(false);
        setScrollingCacheEnabled(false);
        setDivider(new BitmapDrawable(BitmapFactory.decodeResource(getResources(),atts.getResourceId(1, R.drawable.theme_default_divider))));
        setFocusableInTouchMode(true);
        setOnTouchListener(new OnTouchListener(){
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				return gestureDetector.onTouchEvent(event);
			}});
        
        atts.recycle();

        viewFlipper = (ViewFlipper)((TweetsListActivity)context).findViewById(R.id.flipper);
        slideLeftIn = AnimationUtils.loadAnimation(context, R.anim.slide_left_in);
        slideLeftOut = AnimationUtils.loadAnimation(context, R.anim.slide_left_out);
        slideRightIn = AnimationUtils.loadAnimation(context, R.anim.slide_right_in);
        slideRightOut = AnimationUtils.loadAnimation(context, R.anim.slide_right_out);
        
        setOnFocusChangeListener(new OnFocusChangeListener(){

			@Override
			public void onFocusChange(View arg0, boolean arg1) {
				if(arg1 == true){
					((TweetsListActivity)mCtx).SetWindowTitle();
				}
			}
        	
        });
        
        gestureDetector = new GestureDetector(new MyGestureDetector());
	}

    class MyGestureDetector extends SimpleOnGestureListener {
    	
    	@Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            try {
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;
				
                if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                	viewFlipper.setInAnimation(slideLeftIn);
                    viewFlipper.setOutAnimation(slideLeftOut);
                	viewFlipper.showNext();
                	return true;
                }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                	viewFlipper.setInAnimation(slideRightIn);
                    viewFlipper.setOutAnimation(slideRightOut);
                	viewFlipper.showPrevious();
                	return true;
                }
            } catch (Exception e) {
                // nothing
            }
            
            return false;
        }
    }  
}
